/**
 * Title: UploadRequestParams.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2013-8-15 下午12:00:19
 * Version 1.0 
 */
package com.luoxudong.phonenumber.service.net.http.asynchttp;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Map;

import com.luoxudong.phonenumber.service.net.http.filetransfer.manager.BlockStreamBody;
import com.luoxudong.phonenumber.service.net.http.filetransfer.vo.UploadData;
import com.luoxudong.phonenumber.service.net.http.httpclient.HttpEntity;
import com.luoxudong.phonenumber.service.net.http.mime.HttpMultipartMode;
import com.luoxudong.phonenumber.service.net.http.mime.MultipartEntity;
import com.luoxudong.phonenumber.service.net.http.mime.content.StringBody;
import com.luoxudong.phonenumber.service.utils.EvtLog;

/** 
 * ClassName: UploadRequestParams
 * Description:上传请求参数
 * Create by 罗旭东
 * Date 2013-8-15 下午12:00:19
 */
public class UploadRequestParams extends RequestParams{
	private static final String TAG = UploadRequestParams.class.getSimpleName();
	private static final String KEY_NAME = "filedata";
	
	private UploadData uploadData = null;
	private String filePath = null;
	
	public UploadData getUploadData() {
		return uploadData;
	}

	public void setUploadData(UploadData uploadData) {
		this.uploadData = uploadData;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	@Override
	public HttpEntity getEntity() {
		MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
		//long l = multipartEntity.getContentLength();
		Map<String, String> uploadStringParams = uploadData.getUploadStringParams();
		
		if (uploadStringParams != null)
		{
			for (String key : uploadStringParams.keySet())
			{
				try
				{
					multipartEntity.addPart(key, new StringBody( uploadStringParams.get(key), Charset.forName("UTF-8")));
				}
				catch (UnsupportedEncodingException e)
				{
					EvtLog.e(TAG, e.toString());
				}
			}
		}
		
		UploadHttpResponseHandler uploadHttpResponseHandler = (UploadHttpResponseHandler)responseHandler;
		
		BlockStreamBody blockStreamBody = new BlockStreamBody(uploadHttpResponseHandler, new File(filePath), uploadData.getStartOffset(),uploadData.getBlockSize());
		multipartEntity.addPart(KEY_NAME, blockStreamBody);
		
		return multipartEntity;
	}
}
